<?php
/**
 * Creator class, create new players/characters
 * @package char
 * @author alcaitiff
 */
define('AMUD_INITIAL_STRENGTH', 5);
define('AMUD_INITIAL_INTELLIGENCE', 5);
define('AMUD_INITIAL_DEXTERITY', 5);
define('AMUD_INITIAL_VITALITY', 5);
define('AMUD_INITIAL_LIFE', 20);
define('AMUD_INITIAL_MANA', 20);
define('AMUD_INITIAL_XP', 0);
define('AMUD_INITIAL_ROOM', 6);
class Creator extends ExportableObject{
    
    /**
     *
     * @var string
     */
    protected $message;
    
    /**
     * Try to create a new player/character
     * @param string $user
     * @param string $pass
     * @param string $email
     * @param string $char
     */
    public function register($user,$pass,$email,$char){
        if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
            $this->message='Invalid e-mail';
            return false;
        }
        if(!$this->verifyUser($user)){
            $this->message='Invalid user name';
            return false;
        }
        if(!$this->verifyCharacter($char)){
            $this->message='Invalid character name';
            return false;
        }
        if(!($this->saveUser($user,$pass,$email) && $this->saveChar($user,$char,AMUD_INITIAL_STRENGTH,AMUD_INITIAL_DEXTERITY,AMUD_INITIAL_VITALITY,AMUD_INITIAL_INTELLIGENCE,AMUD_INITIAL_XP,AMUD_INITIAL_LIFE,AMUD_INITIAL_MANA,AMUD_INITIAL_ROOM))){
            $this->message='Error saving, please try again later';
            return false;
        }
        //Add the role player lvl 1
        $this->charAddRoles($char);
        //Add the status natural regeneration
        $this->charAddStatus($char);
        return true;
    }
    //***********************************************************************************************************
    //					DATABASE METHODS
    //***********************************************************************************************************
    /**
     * Verify the user existence
     * @param string $user
     * @return boolean
     */
    private function verifyUser($user){
        $ret=false;
        $dbo=new DB();
		$db=$dbo->db;
		$stmt = $db->prepare(	'SELECT `user` FROM `users`  '.
								'WHERE user = ? '.
								'LIMIT 1'
								);
	    $stmt->bind_param("s", $user);
	    $stmt->execute();
        if(!$stmt->fetch()){
            $ret=true;
        }
  		$stmt->close();
		return $ret;
    }
    /**
     * Verify the character existence
     * @param string $char
     * @return boolean
     */
    private function verifyCharacter($char){
        $ret=false;
        $dbo=new DB();
		$db=$dbo->db;
		$stmt = $db->prepare(	'SELECT `char_id` FROM `char`  '.
								'WHERE `char_id` = ? '.
								'LIMIT 1'
								);
	    $stmt->bind_param("s", $char);
	    $stmt->execute();
        if(!$stmt->fetch()){
            $ret=true;
        }
	    $stmt->close();
		return $ret;
    }
    /**
     * Try to save the new user
     * @param string $user
     * @param string $pass
     * @param string $email
     * @return boolean
     */
    private function saveUser($user,$pass,$email){
        $ret=false;
        $dbo=new DB();
		$db=$dbo->db;
		$stmt = $db->prepare(	'INSERT INTO `users`  '.
								'(`user`,`email`,`password`) '.
                                'VALUES (?,?,?)'
								);
	    $stmt->bind_param("sss", $user,$email,$pass);
	    $stmt->execute();
        if($stmt->affected_rows==1){
            $ret=true;
        }
	    $stmt->fetch();
  		$stmt->close();
        return $ret;
    }
    /**
     * Try ti save the new character
     * @param string $user
     * @param string $char
     * @return boolean
     */
    private function saveChar($user,$char,$strength,$dexterity,$vitality,$intelligence,$experience,$life,$mana,$room){
        $ret=false;
        $dbo=new DB();
		$db=$dbo->db;
		$stmt = $db->prepare(	'INSERT INTO `char`  '.
								'(`user`,`char_id`,`strength`,`dexterity`,`vitality`,`intelligence`,`experience`,`life`,`mana`,`room`) '.
                                'VALUES (?,?,?,?,?,?,?,?,?,?)'
								);
	    $stmt->bind_param("ssiiiiiiii", $user,$char,$strength,$dexterity,$vitality,$intelligence,$experience,$life,$mana,$room);
	    $stmt->execute();
        if($stmt->affected_rows==1){
            $ret=true;
        }
	    $stmt->fetch();
  		$stmt->close();
        return $ret;
    }
    /**
     * Add the player role to the new char
     * @param string $char
     */
    private function charAddRoles($char){
        $dbo=new DB();
		$db=$dbo->db;
		$stmt = $db->prepare(	'INSERT INTO `characters_roles`  '.
								'(`char_id`,`role`,`level`) '.
                                'VALUES (?,"player",1)'
								);
	    $stmt->bind_param("s", $char);
	    $stmt->execute();
	    $stmt->fetch();
  		$stmt->close();
    }
    /**
     * Add status to the new char
     * @param string $char
     */
    private function charAddStatus($char){
        $dbo=new DB();
		$db=$dbo->db;
		$stmt = $db->prepare(	'INSERT INTO `character_status`  '.
								'(`char_id`,`status`,`level`) '.
                                'VALUES (?,"natural regeneration",1)'
								);
	    $stmt->bind_param("s", $char);
	    $stmt->execute();
	    $stmt->fetch();
  		$stmt->close();
    }
}